package database.DAO;

import database.JDBCUtils.Druid;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BasicDAO<T> {
    private static QueryRunner queryRunner =new QueryRunner();
    public  int update(String sql,Object...params){
        Connection conn=null;
        try {
             try {
                 conn = Druid.getConnection();
                 int update = queryRunner.update(conn,sql, params);
                 return update;
             } finally {
                if (conn!=null){
                    conn.close();
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

//    public int insert(String sql,Class<T> clazz,Object...params){
//        Connection conn=null;
//        try {
//            try {
//                conn=Druid.getConnection();
//                T insert = queryRunner.insert(conn, sql, new BeanHandler<>(clazz), params);
//                System.out.println(insert.getClass());
//                return 0;
//            }finally {
//                if (conn!=null){
//                    conn.close();
//                }
//            }
//
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//    }
    public List<T> queryBeanList(String sql, Class<T> clazz, Object...params){
        Connection conn=null;
        try {
             try {
                 conn = Druid.getConnection();
                 return queryRunner.query(conn,sql,new BeanListHandler<>(clazz),params);
             }finally {
                 if (conn!=null) {
                     conn.close();
                 }
             }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public T queryBean(String sql, Class<T> clazz, Object...params){
        Connection conn=null;
        try {
            try {
                conn = Druid.getConnection();
                return queryRunner.query(conn,sql,new BeanHandler<>(clazz),params);
            }finally {
                if (conn!=null) {
                    conn.close();
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Object queryScalar(String sql, Object... parameters) {
        Connection conn = null;
        try {
            try {
                conn =Druid.getConnection();
                return  queryRunner.query(conn, sql, new ScalarHandler(), parameters);
            }finally {
                conn.close();
            }
        } catch (SQLException e) {
            throw  new RuntimeException(e); //将编译异常->运行异常 ,抛出
        }
    }

    public  List<Object>  queryColumnList(String sql,Object...params){
        Connection conn=null;
        try {
            try {
                conn = Druid.getConnection();
                return (List<Object>)queryRunner.query(conn, sql, new ColumnListHandler(), params);
            }finally {
                if (conn!=null){
                    conn.close();
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public List<Object[]> queryArrayList(String sql,Object...params){
        Connection conn=null;
        try {
            try {
                conn = Druid.getConnection();
                return queryRunner.query(conn, sql, new ArrayListHandler(), params);
            }finally {
                if (conn!=null){
                    conn.close();
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
